<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>SWF Utility</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>SWF Utility</h1>

    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
   <p>The YUI SWF Utility provides a standardized method for embedding the Adobe Flash player in web pages. Specifically, the SWF Utility:
	<ol>
		<li> Creates browser-specific, standards-compliant markup for correctly embedding an instance of Flash player plugin (in browsers with Netscape Plugin Architecture), or an instance of Flash Player ActiveX control (in Internet Explorer).</li>
		<li> Allows developers to set a requirement for the minimum version of Flash player installed on the end user's computer.
		<li> Provides a convenient single method instantiation of the embedded player properties and variables.</li>
		<li> Wraps the ExternalInterface API of the Flash player to allow method calls on specific player instances.</li>
		<li> In conjunction with the YUIBridge ActionScript API, allows Flash applications to dispatch events that get converted to native YUI events.</li>
	</ol>
	
	<p><strong>Important usage notes:</strong></p><ul>

	<li>The SWF Utility is engineered to only work with A-grade browsers. Because it allows only standards-compliant markup, the utility will not function properly in older browser versions.</li>
	<li>The <code>swfReady</code> event provided by the SWF Utility is only available when the Flash application being embedded uses the YUIBridge ActionScript library (see below).</li>
  	<li>The current recommended version of the Flash Player is 10.1, available at the <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player Download Center</a>.</li>
  </ul>
</div>

<h2 id="getting-started">Getting Started</h2>

<p>
To include the source files for SWF Utility and its dependencies, first load
the YUI seed file if you haven't already loaded it.
</p>

<pre class="code prettyprint">&lt;script src=&quot;http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.4.1&#x2F;build&#x2F;yui&#x2F;yui-min.js&quot;&gt;&lt;&#x2F;script&gt;</pre>


<p>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
YUI will automatically load any dependencies required by the modules you
specify.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a new YUI instance and populate it with the required modules.
YUI().use(&#x27;swf&#x27;, function (Y) {
    &#x2F;&#x2F; SWF Utility is available and ready for use. Add implementation
    &#x2F;&#x2F; code here.
});</pre>


<p>
For more information on creating YUI instances and on the
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
documentation for the <a href="../yui/index.html">YUI Global object</a>.
</p>

				
<h2 id="using-the-swf-utility">Using the SWF Utility</h2>

<p>This section describes how to use the SWF Utility in further detail.


<h3 id="anatomy-of-the-swf-utility">Anatomy of the SWF Utility</h3>

<p>The SWF Utility uses the browser information from the <code>Y.ua</code> variable to generate the appropriate markup for embedding the Flash player. The markup that the SWF Utility generates is entirely based on the <code>&lt;object&gt;</code> tag, and therefore will not work in older browsers that use the non-standard <code>&lt;embed&gt;</code> tag.
</p>
<p>In addition, the SWF Utility also determines the version of the Flash player (using YUI's <code>swfdetect</code> library), and places that information in <code>Y.ua.flash</code> as a period-delimited string (MajorVersion.MinorVersion.Revision). This information is used by SWF utility to prevent Flash Player embedding if the version of user's installed Flash player does not match that required by the developer.</p>				
<p>
   While the SWF Utility provides a direct access to the instance of the Flash player it encapsulates, it is recommended that all communication to that instance occur via the methods provided by the SWF Utility (see below.)
</p>

<h3 id="configuring-and-instantiating-the-swf-utility">Configuring and Instantiating the SWF Utility</h3>
<p>For simplicity, all configuration of the embedded Flash player instance happens in the SWF Utility at construction time, and therefore, all configuration parameters must be provided as arguments to the SWF constructor.</p>
<p>By default, the SWF constructor requires only two arguments &ndash; the reference to the container in which the instance of the Flash player should be placed, and the URL of the swf application that the Flash player instance should load. Note that the Flash player instance created by the SWF Utility always adopts the size of the parent container it is placed into; therefore, setting the size of the Flash player is as simple as setting the size of its container.</p>                 
<p>To put an instance of the Flash player on the page, simply create a new instance of <code>Y.SWF</code> and provide it with the reference to the container in which it should be placed, as well as the URL of the swf file that needs to be loaded:</p>
<pre class="code prettyprint">YUI({...}).use(&#x27;swf&#x27;,function (Y) {

    &#x2F;&#x2F; Default everything
	var myswf = new Y.SWF(&quot;#swfContainer&quot;, &quot;http:&#x2F;&#x2F;example.com&#x2F;example.swf&quot;);

});</pre>


<p>If you would like to specify additional parameters or pass arbitrary variables to the SWF, you can provide the constructor with the third argument, a paramter Object:</p>
<pre class="code prettyprint">var params = {version: &quot;9.0.115&quot;,
	          fixedAttributes:
			  {allowScriptAccess:&quot;always&quot;, allowNetworking:&quot;all&quot;},
			  flashVars:
			  {foo: &quot;bar&quot;, foo1: &quot;bar1&quot;}
			 };
var myswf = new Y.SWF(&quot;#swfContainer&quot;, &quot;http:&#x2F;&#x2F;example.com&#x2F;example.swf&quot;, params);</pre>


<p>The parameter object may contain the following possible properties:</p>


                <table>
                <thead>
                    <tr>
                        <th>Property</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><code>version</code></td>
                        <td>A period-delimited string of three numbers specifying the minimum version of Flash player that must be installed on the user's machine.</td>
                    </tr>
                    <tr>
                        <td><code>fixedAttributes</code></td>
                        <td>An object with a set of configuration variables specific to the Flash player. The list of all possible configuration variables and their values, provided by Adobe, can be found <a href="http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7f18.html">here</a>.</td>
                    </tr>
                    <tr>
                        <td><code>flashVars</code></td>
                        <td>An object with a set of variables to pass to the Flash player. These variables are made available to the Flash application at initialization time, in the ActionScript's <code>Stage.loaderInfo.parameters</code> container.</td>
                    </tr>
                </tbody>
                </table>
<h3 id="calling-methods-on-the-instance-of-flash-player">Calling Methods on the instance of Flash player</h3>

<p>The SWF Utility provides a wrapper for calling methods that have been exposed to JavaScript from within the Flash application. This allows the developer to call the methods without having to directly reference the private pointer to the instance of the Flash player contained in the SWF Utility. To call a method <code>foo(arg1, arg2)</code> on an instance of the Flash player, do the following:

<pre class="code prettyprint">YUI({...}).use(&#x27;swf&#x27;,function (Y) {

	var myswf = new Y.SWF(&quot;#swfContainer&quot;, &quot;http:&#x2F;&#x2F;example.com&#x2F;example.swf&quot;);
	myswf.callSWF(&quot;foo&quot;, [arg1, arg2]);	

});</pre>


<p>Note that, since Flash player is run as a separate thread, the existence of its instance is not guaranteed when the method call on it is made. To make sure that Flash is ready to accept incoming method calls, it's best to have the Flash player make a call to JavaScript announcing that it has finished initializing. While there is a number of ways to do that, YUI hybrid components use the YUIBridge ActionScript library to automate this process. See the next section for notes on its usage.</p>

<h3 id="working-with-the-yuibridge-actionscript-library">Working with the YUIBridge ActionScript Library</h3>

<p>YUIBridge ActionScript library creates a standardized method for sending YUI events from a Flash application. In an ActionScript application, an instance of YUIBridge is created as follows:</p>
	
<pre class="code prettyprint">import com.yahoo.util.YUIBridge

...

&#x2F;&#x2F; The scope for &#x27;this&#x27; should be either the top-level application
&#x2F;&#x2F; or a Sprite that has been placed on the stage.

var yuiBridge:YUIBridge = new YUIBridge(this.stage);</pre>


<p>Instead of using <code>ExternalInterface.addCallback</code> on each method that needs to be exposed to JavaScript, you can instead call a collective <code>addCallbacks()</code> method available in YUIBridge:</p>

<pre class="code prettyprint">import com.yahoo.util.YUIBridge

...

&#x2F;&#x2F; The scope for &#x27;this&#x27; should be either the top-level application
&#x2F;&#x2F; or a Sprite that has been placed on the stage.

var yuiBridge:YUIBridge = new YUIBridge(this.stage);
yuiBridge.addCallbacks({externalMethodName:internalMethod, externalMethodName2:internalMethod2});

});</pre>


<p>To dispatch YUI events (fired by the instance of the SWF Utility on the JavaScript end, you can call YUIBridge's <code>sendEvent</code> method:</p>

<pre class="code prettyprint">import com.yahoo.util.YUIBridge

...

&#x2F;&#x2F; The scope for &#x27;this&#x27; should be either the top-level application
&#x2F;&#x2F; or a Sprite that has been placed on the stage.

var yuiBridge:YUIBridge = new YUIBridge(this.stage);
yuiBridge.sendEvent({type:&#x27;someevent&#x27;, payload:&#x27;some payload&#x27;});
});</pre>


<p>In addition to any custom events you dispatch, YUIBridge also fires a <code>swfReady</code> event at initialization time, announcing that the Flash player is ready for communication with JavaScript:</p>
<pre class="code prettyprint">YUI({...}).use(&#x27;swf&#x27;,function (Y) {

	var myswf = new Y.SWF(&quot;#swfContainer&quot;, &quot;http:&#x2F;&#x2F;example.com&#x2F;example.swf&quot;);
	myswf.on(&#x27;swfReady&#x27;, init);
	
	function init() {
		&#x2F;&#x2F; Start communicating with the Flash player instance here
		myswf.callSWF(&quot;foo&quot;, [arg1, arg2]);
	}

});</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            
                <div id="toc" class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Table of Contents</h2>
                    </div>

                    <div class="bd">
                        <ul class="toc">
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#using-the-swf-utility">Using the SWF Utility</a>
<ul class="toc">
<li>
<a href="#anatomy-of-the-swf-utility">Anatomy of the SWF Utility</a>
</li>
<li>
<a href="#configuring-and-instantiating-the-swf-utility">Configuring and Instantiating the SWF Utility</a>
</li>
<li>
<a href="#calling-methods-on-the-instance-of-flash-player">Calling Methods on the instance of Flash player</a>
</li>
<li>
<a href="#working-with-the-yuibridge-actionscript-library">Working with the YUIBridge ActionScript Library</a>
</li>
</ul>
</li>
</ul>
                    </div>
                </div>
            

            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
